#
#   ResourceManager.py
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; version 2 of the License.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   Copyright
#   Author: Nicholas F. Hoover
#   Contributors: Salvatore S. Gionfriddo
#   Created: 2007.06.18
#   Last Modified: 2007.07.10
#

import pygame, os

class ResourceManager(object):

   BASE_NAME = 0
   FRAMES = 1
   ANIMATION_LIST = [
      ('EG_Idle',4),
      ('EG_WalkSide',8),
      ('EG_Punch',3),
      ('EG_Kick',5),
      ('EG_Flame',4),
      ('EG_Eat', 6),
      ('EG_Dead', 1),
      ('BasicKid_Run',2),
      ('BasicKid_Bite',2),
      ('BasicKid_Dead',1),
      ('FastKid_Run',2),
      ('FastKid_Bite',2),
      ('FastKid_Dead',1),
      ('Flame', 3),
      ('Drugs', 1),
      ('Pizza', 1),
      ('Indian', 3),
      ]  

   soundList = [
      'EG_Kick',
      'EG_Punch',
      'EG_Eat',
      'Kid_Crunch',
      'Kid_Bite',
      'Kid_Die',
      'Flame',
      ]

   NUM_SONGS = 3
   (INTRO_SONG,
    GAME_SONG,
    DEAD_SONG) = range(NUM_SONGS)
   
   SONG_NAMES = [
      'intro_song',
      'game_song',
      'dead_song',
      ]

   USE_MUSIC = True
   USE_OGG = True
   USE_MIDI = False

   def __init__(self):
      self.animations = {}
      self.sounds = {}

      if ResourceManager.USE_OGG:
         self.songExtension = '.ogg'
      elif ResourceManager.USE_MIDI:
         self.songExtension = '.mid'
      else:
         self.songExtension = ''


   def loadResources(self):
      self.loadAnimations()
      self.loadSounds()

   def getSplashScreen(self):
      return self.loadImage('splashscreen.png')
   

##########
# IMAGES #      
##########

# Name: loadAnimations
# Description:
#      Iterates over the animation list, loading the frames of each animation from files and adding them (and their horizontal flips) to the dictionary of animations.
#       File animations are facing right, and are flipped to provide left facing animations.
   def loadAnimations(self):
      for animation in self.ANIMATION_LIST:
         animRight = []
         animLeft = []
         for frame in range(animation[self.FRAMES]):
            fullname = animation[self.BASE_NAME] + '_' + str(frame)
            image = self.loadImage(fullname + '.png', -1)
            animRight.append(image)
            animLeft.append(pygame.transform.flip(image,True,False))
         self.animations[animation[self.BASE_NAME] + "_Right"] = animRight
         self.animations[animation[self.BASE_NAME] + "_Left"] = animLeft


# Name: loadImage
# Description:
#      Loads an image from a file in the subdirectory 'data'. Exits program if image is not found.
#      Sets the colorkey (transparent color) as the pure white if colorkey = -1
   def loadImage(self, name, colorkey=None):
       fullname = os.path.join('data', 'images', name)
       try:
           image = pygame.image.load(fullname)
       except pygame.error, message:
           print 'Cannot load image:', name
           raise SystemExit, message
       image = image.convert()

       if colorkey is not None:
          if colorkey is -1:
             colorkey = (255,255,255) #Makes pure white always the transparent color
          image.set_colorkey(colorkey, pygame.RLEACCEL)
          
       return image

   def getAnimation(self, name):
      return self.animations[name]

   def getImage(self, name, frame):
      return self.animations[name][frame]
         
                     
##########
# SOUNDS #
##########

   def loadSounds(self):
      for sound in self.soundList:
         fullname = os.path.join('data', 'sounds', sound) + '.wav'
         try:
            loadedSound = pygame.mixer.Sound(fullname)
         except pygame.error, message:
            print 'Cannot load sound:', sound, 'from', fullname
            raise SystemExit, message
         if sound != 'EG_Flame': #HACK - this sound is too quiet currently so don't reduce the volume
            loadedSound.set_volume(0.5)
         self.sounds[sound] = loadedSound


   def getSound(self,name):
      return self.sounds[name]


   def loadSong(self, song):
      if self.USE_MUSIC:
         fullname = os.path.join('data', 'sounds', self.SONG_NAMES[song]) + self.songExtension
         
         try:
            pygame.mixer.music.load(fullname)
         except pygame.error, message:
            print 'Cannot load song:', fullname
            raise SystemExit, message
         pygame.mixer.music.set_volume(1.0)
